#############################################################
# Copyright (c) 2015, John Lee <furious_tauren@163.com>
# Thu Apr 16 09:25:01 CST 2015
#############################################################

# -----------------------------------------------------------

TOPDIR := $(shell pwd)
-include $(TOPDIR)/config.mk

all: boot linux rootfs

# ===========================================================
#  Build u-boot
# ===========================================================
PHONY := boot
#MACHDIR := $(U_BOOT)/arch/arm
MACHDIR := $(U_BOOT)/arch/arm/cpu/armv7/mx6
boot:
	$(Q)echo -en ". ============================== .\n"
	$(Q)echo -en "| \tBuilding u-boot ...\n"
	$(Q)echo -en "^ ============================== ^\n"
	$(Q)$(CP) boot/* $(U_BOOT)
ifeq ($(shell echo `grep "board/taro/Kconfig" $(MACHDIR)/Kconfig`),)
	$(Q)sed -i -f boot/Kconfig.sed $(MACHDIR)/Kconfig
endif
	$(Q)$(MAKE) -C $(U_BOOT) taro_defconfig
	$(Q)$(MAKE) -C $(U_BOOT) -j $(CPU_NR)
	$(Q)$(CP) $(U_BOOT)/u-boot.imx $(INSTDIR)
	$(CP) $(U_BOOT)/u-boot.imx $(NFSDIR)

# ===========================================================
#  Build kernel
# ===========================================================
PHONY += linux
linux:
	$(Q)echo -en ". ============================== .\n"
	$(Q)echo -en "| \tBuilding linux ...\n"
	$(Q)echo -en "^ ============================== ^\n"
	$(Q)$(CP) kernel/arch $(KERNDIR)
ifeq ($(shell echo `grep "imx6dl-taro.dtb" $(KERNDIR)/arch/arm/boot/dts/Makefile`),)
	$(Q)sed -i '/imx6dl-udoo.dtb/i\\timx6dl-taro.dtb \\' $(KERNDIR)/arch/arm/boot/dts/Makefile
endif
	$(Q)$(CP) kernel/taro_defconfig $(KERNDIR)/arch/arm/configs
	$(Q)$(MAKE) -C $(KERNDIR) taro_defconfig
	$(Q)$(MAKE) -C $(KERNDIR) LOADADDR=0x10800000 uImage -j $(CPU_NR)
	$(Q)$(MAKE) -C $(KERNDIR) dtbs modules modules_install
	$(Q)find $(MODDIR) -name build -o -name source | xargs -l1 rm -rf
	$(Q)$(CP) $(KERNDIR)/arch/arm/boot/uImage $(INSTDIR)
	$(Q)$(CP) $(KERNDIR)/arch/arm/boot/dts/imx6dl-taro.dtb $(INSTDIR)
	$(CP) $(KERNDIR)/arch/arm/boot/uImage $(NFSDIR)
	$(CP) $(KERNDIR)/arch/arm/boot/dts/imx6dl-taro.dtb $(NFSDIR)

PHONY += dts
dts:
	$(Q)$(CP) kernel/arch $(KERNDIR)
	$(Q)$(MAKE) -C $(KERNDIR) dtbs
	$(Q)$(CP) $(KERNDIR)/arch/arm/boot/dts/imx6dl-taro.dtb $(INSTDIR)
	$(CP) $(KERNDIR)/arch/arm/boot/dts/imx6dl-taro.dtb $(NFSDIR)

# ===========================================================
#  Build rootfs
# ===========================================================
PHONY += rootfs skeleton ramdisk yaffs2 ubifs
rootfs: ramdisk ubifs ;

ramdisk yaffs2 ubifs: skeleton ;
	$(Q)fakeroot -- $(BIN)/$@.gen
	$(Q)$(CP) $(PUBDIR)/$@ $(INSTDIR)
	$(Q)$(CP) $(PUBDIR)/$@ $(NFSDIR)

skeleton:
	$(Q)echo -en ". ============================== .\n"
	$(Q)echo -en "| \tSetup rootfs ...\n"
	$(Q)echo -en "^ ============================== ^\n"
	$(Q)$(CP) $(FSDIR)/configs/bbconfig $(BBDIR)/.config
	$(Q)$(MAKE) -C $(BBDIR) -j $(CPU_NR)
	$(Q)$(MAKE) CONFIG_PREFIX=$(FAKEROOT) -C $(BBDIR) install
	$(Q)tar -jxf $(FSDIR)/skeleton.tar.bz2 -C $(FAKEROOT)
	$(Q)$(CP) $(SYSLIB)/* $(FAKEROOT)/lib/
	$(Q)find $(FAKEROOT)/lib -type f -a -name *.so -exec $(STRIP) {} \;
	$(Q)find $(FAKEROOT)/lib -type f -a -name *.so.* -exec $(STRIP) {} \;
	$(Q)find $(FAKEROOT) -type d | xargs chmod 755
	$(Q)find $(FAKEROOT) -type f -name *.so | xargs chmod 755
	$(Q)find $(FAKEROOT) -type f -name *.so.* | xargs chmod 755

# ===========================================================
#  Other stuff
# ===========================================================
PHONY += clean
clean:
	$(Q)rm -fr $(PUBDIR)
	$(Q)$(MAKE) -C $(U_BOOT) distclean
	$(Q)$(MAKE) -C $(KERNDIR) distclean
	$(Q)$(MAKE) -C $(BBDIR) distclean

.PHONY: $(PHONY)

